//
//  RegisterViewController.m
//  豆瓣_精简版
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegisterView.h"
#import "DataBase.h"
@interface RegisterViewController ()

@property(nonatomic,retain)RegisterView *registerName;
@property(nonatomic,retain)RegisterView *registerPassword;
@property(nonatomic,retain)RegisterView *registerRePassword;
@property(nonatomic,retain)RegisterView *registerAddress;
@property(nonatomic,retain)RegisterView *registerNumber;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"注册";
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"注册" style:UIBarButtonItemStyleDone target:self action:@selector(registerAction:)];
    
    
    self.registerName = [[RegisterView alloc]initWithFrame:CGRectMake(0, 90, 375, 30)];
    self.registerName.label.text = @"用户名:";
    self.registerName.textField.placeholder = @"请输入用户名:";
    [self.view addSubview:self.registerName];
    
    self.registerPassword = [[RegisterView alloc]initWithFrame:CGRectMake(0, 150, 375, 30)];
    self.registerPassword.label.text = @"密码:";
    self.registerPassword.textField.placeholder = @"请输入密码:";
    [self.view addSubview:self.registerPassword];
    
    self.registerRePassword = [[RegisterView alloc]initWithFrame:CGRectMake(0, 210, 375, 30)];
    self.registerRePassword.label.text = @"确认密码:";
    self.registerRePassword.textField.placeholder = @"再次确认密码:";
    [self.view addSubview:self.registerRePassword];
    
    self.registerAddress = [[RegisterView alloc]initWithFrame:CGRectMake(0, 270, 375, 30)];
    self.registerAddress.label.text = @"邮箱:";
    self.registerAddress.textField.placeholder = @"请输入邮箱:";
    [self.view addSubview:self.registerAddress];
    
    self.registerNumber = [[RegisterView alloc]initWithFrame:CGRectMake(0, 330, 375, 30)];
    self.registerNumber.label.text = @"联系方式:";
    self.registerNumber.textField.placeholder = @"请输入联系方式:";
    [self.view addSubview:self.registerNumber];
    
//    [[DataBase shareDataBase]dropTable:@"user"];
    
}

- (void)registerAction:(UIBarButtonItem *)bar{
    
    NSString *name = self.registerName.textField.text;
    NSString *password = self.registerPassword.textField.text;
    
    NSMutableArray *array = [[DataBase shareDataBase]selectUserData:name];
    if (name.length == 0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名不能为空" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
            
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        }];
    }else{
        
        if (array.count == 0) {
            //记录谁登陆的和插入数据
            [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"state"];
            [[NSUserDefaults standardUserDefaults]setObject:name forKey:@"user"];
            [[DataBase shareDataBase]insertUser:name password:password];
            [self dismissViewControllerAnimated:YES completion:^{
                
                
            }];
        }else{
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名已注册" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:^{
                
                [self dismissViewControllerAnimated:YES completion:^{
                    
                }];
            }];
        }
        
        
    }
    
    
    
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
